package com.example.onlinetaxi.Thread;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.dto.DataResponse;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.ui.activity.MainPage.MainActivity;
import com.example.onlinetaxi.util.OKHttpUtil.GetDataResponse;
import com.example.onlinetaxi.util.OKHttpUtil.HttpUrl;
import com.example.onlinetaxi.util.OKHttpUtil.OKHttpUtil;
import com.example.onlinetaxi.util.TimeUtil;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class PassengerGetOrderStateThread {
    public static Thread thread = null;

    // 线程终止变量
    public static Boolean finished = false;

    // 用来判断订单是否结束
    public static class GetCompletedOrderState implements Runnable {

        private Context fromContext;
        private Class toClass;

        public GetCompletedOrderState(Context fromContext, Class toClass) {
            this.fromContext = fromContext;
            this.toClass = toClass;
        }

        @Override
        public void run() {
            // 轻微沉睡，保证司机修改订单状态
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 存入订单id
            Map<String, String> map = new HashMap<>();
            map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());

            // 使用while循环不断向后端发送订单状态获取信息
            while (true) {
                String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "completedOrder", "getOrderState");

                DataResponse dataResponse = GetDataResponse.getDataResponse(response);

                if (LocalStorage.currentOrderId == null) {
                    break;
                }

                if (dataResponse.getCode().equals("200")) {
                    // 如果订单状态为完成
                    if (dataResponse.getData().get("state").toString().equals("7")) {
                        // 获取订单
                        map.clear();
                        map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());

                        String responseStr = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "completedOrder", "getCompletedOrder");

                        DataResponse dataResponse2 = GetDataResponse.getDataResponse(responseStr);

                        // 配置合适订单
                        Gson gson = new Gson();
                        LocalStorage.currentOrder = gson.fromJson(dataResponse2.getData().get("order").toString(), CompletedOrderDto.class);

                        // 转换时间格式
                        LocalStorage.currentOrder = TimeUtil.setRightTimeFormat(LocalStorage.currentOrder);
                        // 设置当前订单
                        LocalStorage.currentOrderId = LocalStorage.currentOrder.getId().toString();

                        break;
                    }
                }

                if (finished) {
                    break;
                }

                // 间歇800ms发一次
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


            if (!finished) { // 用户没有取消
                // 跳转到评价界面之前一定要先实例化LocalStorage里面的chosenOrder
                LocalStorage.chosenOrder = LocalStorage.currentOrder;

                // 跳转到等待司机接车界面或评价界面
                Intent intent = new Intent(fromContext, toClass);
                fromContext.startActivity(intent);
                // 销毁当前页面
                Activity activity = (Activity) this.fromContext;
                activity.finish();
            }

            // 销毁线程
            Thread.currentThread().interrupt();
        }
    }

    // 用来对司机接收到的订单发送获取状态请求，判断是否前往终点
    public static class GetReceivedOrderState implements Runnable {

        private Context fromContext;
        private Class toClass;

        public GetReceivedOrderState(Context fromContext, Class toClass) {
            this.fromContext = fromContext;
            this.toClass = toClass;
        }

        @Override
        public void run() {
            // 轻微沉睡，保证司机修改订单状态
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 存入订单id
            Map<String, String> map = new HashMap<>();
            map.put("orderId", LocalStorage.currentOrder.getReceivedOrderId().toString());

            // 使用while循环不断向后端发送订单状态获取信息
            while (true) {
                String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "receivedOrder", "getOrderState");

                DataResponse dataResponse = GetDataResponse.getDataResponse(response);

                if (LocalStorage.currentOrderId == null) {
                    break;
                }

                // 如果订单状态为司机带着乘客去往终点
                if (dataResponse.getData().get("state").toString().equals("4")) {
                    // 修改当前订单状态
                    LocalStorage.currentOrder.setState("4");

                    break;
                }

                if (finished) {
                    break;
                }

                // 间歇800ms发一次
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


            if (!finished) { // 用户没有取消
                // 跳转到等待司机接车界面或评价界面
                Intent intent = new Intent(fromContext, toClass);
                fromContext.startActivity(intent);
                // 销毁当前页面
                Activity activity = (Activity) this.fromContext;
                activity.finish();
            }

            // 销毁线程
            Thread.currentThread().interrupt();
        }
    }

    // 用来发送请求，接收返回数据，进行页面跳转的线程
    public static class GetOrderState implements Runnable {

        private Context fromContext;
        private Class toClass;

        public GetOrderState(Context fromContext, Class toClass) {
            this.fromContext = fromContext;
            this.toClass = toClass;
        }

        @Override
        public void run() {
            // 轻微沉睡，保证订单被传送到
            try {
                Thread.sleep(1500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 存入订单id
            Map<String, String> map = new HashMap<>();
            map.put("orderId", LocalStorage.currentOrderId);

            // 使用while循环不断向后端发送订单状态获取信息
            while (true) {
                String response = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "order", "getOrderState");

                DataResponse dataResponse = GetDataResponse.getDataResponse(response);

                if (LocalStorage.currentOrderId == null) {
                    break;
                }

                // 如果订单状态为receiver/completed
                if (dataResponse.getData().get("state").toString().equals("2")) {
                    map.clear();
                    map.put("orderId", LocalStorage.currentOrderId);

                    // 获取订单
                    String responseStr = OKHttpUtil.postAsyncRequest(HttpUrl.getBaseUrl(), map, "receivedOrder", "getReceivedOrder");

                    DataResponse dataResponse2 = GetDataResponse.getDataResponse(responseStr);

                    // 配置合适订单
                    Gson gson = new Gson();
                    LocalStorage.currentOrder = gson.fromJson(dataResponse2.getData().get("order").toString(), CompletedOrderDto.class);
                    // 转换时间格式
                    LocalStorage.currentOrder = TimeUtil.setRightTimeFormat(LocalStorage.currentOrder);
                    // 设置当前订单
                    LocalStorage.currentOrderId = LocalStorage.currentOrder.getId().toString();

                    break;
                }

                if (finished) {
                    break;
                }

                // 间歇800ms发一次
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


            if (!finished) { // 用户没有取消
                // 跳转到等待司机接车界面
                Intent intent = new Intent(fromContext, toClass);
                fromContext.startActivity(intent);
                // 销毁当前页面
                Activity activity = (Activity) this.fromContext;
                if (!(activity instanceof MainActivity)) {
                    activity.finish();
                }
            }

            // 销毁线程
            Thread.currentThread().interrupt();
        }
    }

}